﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using CosyTrade.Core;
using CosyTrade.Network;
using CosyTrade.Organization;
using CosyTrade.Security;
using CosyTrade.UI;

namespace CosyTrade.Client
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AddinServices services = AddinServices.Instance;
            services.InitalizeAddin();
            services.StartAddin();

            string portalFactoryKey;
            XmlDocument cosyTradeDox = new XmlDocument();
            cosyTradeDox.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CosyTrade.xml"));
            portalFactoryKey = cosyTradeDox.SelectSingleNode("/CosyTrade/Addin/System/PortalFactory").InnerText;

            IRemotingPortalFactory proxyMgr = (IRemotingPortalFactory)services.AddinManager.GetAddin(portalFactoryKey);
            ISecuritySystem securitySys = proxyMgr.RegisterProxy<ISecuritySystem>("SecuritySystem");
            IOrganizationManager organiztionMgr = proxyMgr.RegisterProxy<IOrganizationManager>("OrganizationManager");
            ISystemStatCenter statCenter = (ISystemStatCenter)services.AddinManager.GetAddin("SystemStatCenter");
            LoginForm loginForm = new LoginForm(securitySys, organiztionMgr, statCenter);

            if (loginForm.ShowDialog() == DialogResult.OK)
            {
                MainFormBase mainForm = ((IAddinFormManager)services.AddinManager.GetAddin("AddinFormManager")).MainForm;
                Application.Run(mainForm);
            }

            services.Stop();
        }
    }
}
